<template>
  <div class="app-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>通话管理</span>
          <el-button type="primary" @click="handleMakeCall">
            <el-icon><Phone /></el-icon>
            发起通话
          </el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
        <el-form-item label="主叫号码" prop="caller">
          <el-input
            v-model="queryParams.caller"
            placeholder="请输入主叫号码"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="被叫号码" prop="callee">
          <el-input
            v-model="queryParams.callee"
            placeholder="请输入被叫号码"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="通话状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="通话状态" clearable style="width: 200px">
            <el-option label="进行中" value="active" />
            <el-option label="已结束" value="ended" />
            <el-option label="失败" value="failed" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作按钮区域 -->
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" @click="handleMakeCall">
            <el-icon><Phone /></el-icon>
            发起通话
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button type="success" @click="handleExport">
            <el-icon><Download /></el-icon>
            导出
          </el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table
        v-loading="loading"
        :data="callList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="通话ID" align="center" prop="callId" width="120" />
        <el-table-column label="主叫号码" align="center" prop="caller" />
        <el-table-column label="被叫号码" align="center" prop="callee" />
        <el-table-column label="通话状态" align="center" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="通话时长" align="center" prop="duration" width="120" />
        <el-table-column label="开始时间" align="center" prop="startTime" width="180" />
        <el-table-column label="结束时间" align="center" prop="endTime" width="180" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button 
              v-if="scope.row.status === 'active'"
              type="text" 
              @click="handleHangup(scope.row)"
            >
              <el-icon><PhoneFilled /></el-icon>
              挂断
            </el-button>
            <el-button type="text" @click="handleDetail(scope.row)">
              <el-icon><View /></el-icon>
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <!-- 发起通话对话框 -->
    <el-dialog title="发起通话" v-model="callDialogVisible" width="400px" append-to-body>
      <el-form ref="callFormRef" :model="callForm" :rules="callRules" label-width="80px">
        <el-form-item label="被叫号码" prop="callee">
          <el-input v-model="callForm.callee" placeholder="请输入被叫号码" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="callForm.remark" type="textarea" placeholder="请输入备注信息" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitCall" :loading="callLoading">
            发起通话
          </el-button>
          <el-button @click="callDialogVisible = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 遮罩层
const loading = ref(false)
// 选中数组
const ids = ref<string[]>([])
// 显示搜索条件
const showSearch = ref(true)
// 总条数
const total = ref(0)
// 通话列表数据
const callList = ref<any[]>([])
// 发起通话对话框
const callDialogVisible = ref(false)
// 通话加载状态
const callLoading = ref(false)

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  caller: '',
  callee: '',
  status: ''
})

// 通话表单
const callForm = reactive({
  callee: '',
  remark: ''
})

// 表单校验规则
const callRules = {
  callee: [
    { required: true, message: '被叫号码不能为空', trigger: 'blur' }
  ]
}

// 查询通话列表
const getList = () => {
  loading.value = true
  // 模拟数据
  setTimeout(() => {
    callList.value = [
      {
        callId: 'call_001',
        caller: '1001',
        callee: '1002',
        status: 'active',
        duration: '00:05:30',
        startTime: '2023-12-01 10:30:00',
        endTime: null
      },
      {
        callId: 'call_002',
        caller: '1003',
        callee: '1004',
        status: 'ended',
        duration: '00:02:15',
        startTime: '2023-12-01 09:15:00',
        endTime: '2023-12-01 09:17:15'
      }
    ]
    total.value = 2
    loading.value = false
  }, 1000)
}

// 获取状态类型
const getStatusType = (status: string) => {
  switch (status) {
    case 'active':
      return 'success'
    case 'ended':
      return 'info'
    case 'failed':
      return 'danger'
    default:
      return 'info'
  }
}

// 获取状态文本
const getStatusText = (status: string) => {
  switch (status) {
    case 'active':
      return '进行中'
    case 'ended':
      return '已结束'
    case 'failed':
      return '失败'
    default:
      return '未知'
  }
}

// 搜索按钮操作
const handleQuery = () => {
  queryParams.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryParams.caller = ''
  queryParams.callee = ''
  queryParams.status = ''
  handleQuery()
}

// 多选框选中数据
const handleSelectionChange = (selection: any[]) => {
  ids.value = selection.map(item => item.callId)
}

// 发起通话
const handleMakeCall = () => {
  callForm.callee = ''
  callForm.remark = ''
  callDialogVisible.value = true
}

// 提交通话
const submitCall = () => {
  callLoading.value = true
  setTimeout(() => {
    ElMessage.success('通话发起成功')
    callDialogVisible.value = false
    callLoading.value = false
    getList()
  }, 1000)
}

// 挂断通话
const handleHangup = (row: any) => {
  ElMessageBox.confirm('确认挂断该通话?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('挂断成功')
    getList()
  })
}

// 查看详情
const handleDetail = (row: any) => {
  ElMessage.info('查看通话详情: ' + row.callId)
}

// 导出
const handleExport = () => {
  ElMessage.success('导出成功')
}

// 分页大小改变
const handleSizeChange = (val: number) => {
  queryParams.pageSize = val
  getList()
}

// 页码改变
const handleCurrentChange = (val: number) => {
  queryParams.pageNum = val
  getList()
}

onMounted(() => {
  getList()
})
</script>

<style lang="scss" scoped>
.app-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.mb8 {
  margin-bottom: 8px;
}

.dialog-footer {
  text-align: right;
}
</style>
